<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>滴答办公系统-用户列表</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/media/layui/css/layui.css" ${pageContext.request.contextPath}/media="all">
<script src="${pageContext.request.contextPath}/media/js/jquery.min.js"></script>
<script type="text/javascript">
	   function deleteCourse(){
		   layui.use('table', function() {
			   layer.confirm('是否确认删除学生?',function(index) {
				   layer.msg("删除成功", {icon : 6});
				   layer.msg("删除失败", {icon : 5});
			   });
		   });
	   }
	   
	  
	/*  //跳转页面
		function goPage(pageIndex) {
			location.href="${pageContext.request.contextPath}/user/list/"+pageIndex;
		}
		//改变页面大小
		function changePageSize(obj) {
			var page = obj.value;
			location.href="${pageContext.request.contextPath}/user/list/1?pageSize="+page;
		}
		
		//跳转指定页面
		function tiaozhuan(totalPage) {
			//获取输入框内容
			var pageIndex = $("#page").val();
			var regex = new RegExp(/^[\d]+$/);
			var bool = regex.test(pageIndex);
			if(bool){
				if(pageIndex>totalPage||pageIndex<1){
					alert("输入页码不在范围内！请重新输入~");
					$("#page").focus();
				}else{
					location.href="${pageContext.request.contextPath}/user/list/"+pageIndex;
				}
			}else{
				alert("输入不合法！请重新输入~");
				$("#page").focus();
			}
			
		}
		$(function() {
			$("#pageSize").val(${pageHelper.pageSize});
		}) */
		 function gopage(index) {
			location.href="${pageContext.request.contextPath}/user/show?pageindex="+index;
		}
	</script>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/show?pageindex=1" method="post" id="userForm">
<div class="layui-container">
    <div class="layui-row" style="margin-top: 10px">
        <div class="layui-col-xs3" style="margin-right: 20px">
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">姓名：</label>
                <div class="layui-input-block">
                    <input type="text" id="no" name="username" class="layui-input" placeholder="用户姓名">
                </div>
            </div>
        </div>
        <div class="layui-col-xs3" style="margin-right: 20px">
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">用户：</label>
                <div class="layui-input-block">
                    <select class="layui-input" id="fg" name="locked">
                        <option value="-1"  ${pageHelper.bean.locked==-1?'selected':''}>全部</option>
                        <option value="1"  ${pageHelper.bean.locked==1?'selected':''}>启用</option>
                        <option value="0"  ${pageHelper.bean.locked==0?'selected':''}>禁用</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs2">
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" onclick="searchData()"><i class="layui-icon layui-icon-search">搜索</i></button>
                </div>
            </div>
        </div>
    </div>
</div>
</form>
	<div class="layui-container">
		<table class="layui-table" id="tbdata" lay-filter="tbop">
			<thead>
				<tr>
					<td>序号</td>
					<td>工号</td>
					<td>姓名</td>
					<td>职位</td>
					<td>状态</td>
					<td>操作</td>
				</tr>
			</thead>
			<tbody>
			<c:forEach items="${pagebean.lis}" var="user">
				<tr>
					<td>${user.id}</td>
					<td>${user.usercode}</td>
					<td>${user.username}</td>
					<td>
					  <c:forEach items="${user.roles}" var="r">
					       ${r.name} &nbsp;&nbsp;
					  </c:forEach>
					</td>
					<td><span style="color: #1E9FFF">启用</span></td>
					<td><a class="layui-btn layui-btn-mini" href="#" onclick="showUpdate(${user.id});">编辑</a>
						<a class="layui-btn layui-btn-danger layui-btn-mini" href="javascript:changeState();" lay-event="detail">禁用</a>
						<a class="layui-btn layui-btn-danger layui-btn-mini"
						lay-event="del" onclick="deleteCourse();">删除</a>
					</td>
				</tr>
			</c:forEach>
			</tbody>
		</table>
		<div class="layui-box layui-laypage layui-laypage-default" id="layui-laypage-1">
						   <a href="#" onclick="gopage(1);">首页</a>
				<c:if test="${pagebean.pagein!=1}">
				<a href="#" onclick="gopage(${pagebean.pagein-1});">上一页</a>
				</c:if>
				<c:forEach begin="${pagebean.start}" end="${pagebean.end}" step="1" var="i">
				<c:if test="${pagebean.pagein==i}">
				<a href="#" onclick="gopage(${i});" style="color: red;">${i}</a>
				</c:if>
				<c:if test="${pagebean.pagein!=i}">
				<a href="#" onclick="gopage(${i});">${i}</a>
				</c:if>
				</c:forEach>
				<c:if test="${pagebean.pagein!=pagebean.toalPage}">
				<a href="#" onclick="gopage(${pagebean.pagein+1});">下一页</a>
				</c:if>
				<a href="#" onclick="gopage(${pagebean.toalPage});">尾页</a>
				<span>
				[${pagebean.pagein}/${pagebean.toalPage}]页
				[${pagebean.count}]项
				</span>

						</div>
	</div>
	
	<script src="${pageContext.request.contextPath}/media/layui/layui.js"></script>
	
	<script type="text/javascript">
	   function deleteCourse(){
		   layui.use('table', function() {
			   layer.confirm('是否确认删除学生?',function(index) {
				   layer.msg("删除成功", {icon : 6});
				   layer.msg("删除失败", {icon : 5});
			   });
		   });
	   }
	   
	   //启用禁用
	   function changeState(){
	   	   layui.use('table', function() {
			   layer.confirm('确认禁用吗?',function(index) {
				   layer.msg("操作成功", {icon : 6});
			   });
		   });
	   }
	   
	   var form;
	   
	   //弹出编辑框
	   function showUpdate(id){
		   $.get("${pageContext.request.contextPath}/user/toupdate?uid="+id, null, function(obj) {
			   var user=obj.user;
			   $("#update_id").val(user.id);
			   $("#update_usercode").val(user.usercode);
			   $("#update_username").val(user.username);
			   
			   var roles=obj.roles;
			   for(var j=0;j<roles.length;j++){
				   if(roles[j].flag==true){
				      $("#dv1_update").append("<input type='checkbox' name='rids' value='"+roles[j].id+"' title='"+roles[j].name+"' checked>");
				   }else{
					   $("#dv1_update").append("<input type='checkbox' name='rids' value='"+roles[j].id+"' title='"+roles[j].name+"' >"); 
				   }
			   }
			   //---------------------------------------------------------
			   
			   layui.use('table', function() {
					form=layui.form;
					layer.open({
						area: ['500px', '480px'],
						title: '用户编辑',
						type: 1,
						content: $('#dvlay_update'), //这里content是一个普通的String
						btn: ['更新', '取消'],
						yes:function(index, layero){
							$.ajax({
								url:"${pageContext.request.contextPath}/user/update",
								type:"post",
								dataType:"json",
								data:$("#fm1_update").serialize(),  
								success:function(result){
									if(result==true){
										layer.msg("更新成功", {icon : 6},function(){
											window.location.reload();//刷新页面
										});
									}else{
										layer.msg("更新失败", {icon : 5},function(){
											window.location.reload();//刷新页面
										});
									}
								}
							});
							layer.close(index);//关闭窗口
						},
						cancel: function() {}
					});
		       });
		   });
		   
		   
	   	 
	   }
	   
	</script>
	
	
</body>
</html>

<div style="display: none;margin-top: 10px;width: 480px" id="dvlay_update">
    <form id="fm1_update" class="layui-form layui-form-pane" >
        <div class="layui-form-item" pane >
            <label class="layui-form-label">工号：</label>
            <div class="layui-input-inline">
                <input id="update_id" name="id" type="hidden" class="layui-input">
                <input id="update_usercode" name="usercode" class="layui-input">
            </div>
        </div>
         <div class="layui-form-item" pane >
            <label class="layui-form-label">姓名：</label>
            <div class="layui-input-inline">
                <input id="update_username" name="username" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" pane>
            <label class="layui-form-label">职位：</label>
            <div class="layui-input-inline" id="dv1_update">
                
            </div>
        </div>
    </form>
</div>